package co.fitstart.admin.web.controller.system;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.service.ServiceQrcodeService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.principal.QrcodeType;
import co.fitstart.entity.principal.ServiceQrcode;
import co.fitstart.entity.site.Site;


/**
 * 
 * @author jiecheng
 */
@Controller
@RequestMapping("/qrcodes")
@MenuSetting("menu-system")
public class ServiceQrcodeController {
    
    private static final Logger logger = LoggerFactory.getLogger(ServiceQrcodeController.class);
    
    @Autowired
    @Qualifier("dataService")
    private DataService<ServiceQrcode> qrcodeService;
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Site> siteService;
    
    @Autowired
    private ServiceQrcodeService serviceQrcodeService;

    /**
     * 
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-qrcode-management")
    public ModelAndView list(){
        
        ModelAndView modelAndView = new ModelAndView("system/qrcode/qrcode-list");
        
        List<ServiceQrcode> serviceQrcodes = qrcodeService.listAll(ServiceQrcode.class);
        
        Map<String , List<ServiceQrcode>> serviceQrcodeMappings = CollectionUtils.constructMapValuesFromList(serviceQrcodes, "type");
        
        modelAndView.addObject("qrcodes", serviceQrcodes);
        modelAndView.addObject("serviceQrcodeMappings" , serviceQrcodeMappings);
        
        return modelAndView;
    }
    
    /**
     * Dispaly create qrcode panel
     * 
     * @return
     */
    @RequestMapping(value = "/create" , method = RequestMethod.GET)
    public ModelAndView create(){
        
        ModelAndView modelAndView = new ModelAndView("system/qrcode/qrcode-create");
        
        return modelAndView;
        
    }
    
    /**
     * do create qrcode
     * 
     * @param serviceQrcode
     * @param siteId
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.POST)
    public @ResponseBody String onCreate(ServiceQrcode serviceQrcode,
                                         @RequestParam("siteId") Long siteId){
        
        Result result = new Result();
        
        try {
            
            if(QrcodeType.OFF_LINE.name().equals(serviceQrcode.getType().getName())){

                if(serviceQrcodeService.load(String.valueOf(siteId)) != null){
                    result.fail().message("该门店二维码已存在");
                    return JsonUtils.toJsonString(result);
                }
                
                Site site = siteService.load(Site.class, siteId);
                qrcodeService.save(serviceQrcode.initialize(String.valueOf(siteId) , site.getName()));
            }else{
                
                qrcodeService.save(serviceQrcode);
            }
            
            result.success().message("添加二维码成功");
            
        } catch (Exception e) {
            
            logger.error("#onCreateQrcode - " , e);
            
            result.fail().message("添加二维码失败");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }

    /**
     * Display edit qrcode panel
     * 
     * @param qrcodeId
     * @return
     */
    @RequestMapping(value = "/{qrcodeId}/edit", method = RequestMethod.GET)
    public ModelAndView editQrcode(@PathVariable("qrcodeId") Long qrcodeId){
        
        ModelAndView modelAndView = new ModelAndView("system/qrcode/qrcode-edit");
        
        ServiceQrcode serviceQrcode = qrcodeService.load(ServiceQrcode.class, qrcodeId);
        
        modelAndView.addObject("qrcode", serviceQrcode);
        
        return modelAndView;
    }
    
    /**
     * Do edit qrcode
     * 
     * @param qrcodeId
     * @param value 
     * @return
     */
    @RequestMapping(value = "/{qrcodeId}", method = RequestMethod.PATCH)
    public @ResponseBody String onEditQrcode(@PathVariable("qrcodeId") Long qrcodeId, 
                                             @RequestParam("qrcode") String value ){
        
        Result result = new Result();
        
        try {
            
            ServiceQrcode serviceQrcode = qrcodeService.load(ServiceQrcode.class, qrcodeId);
            
            qrcodeService.update(serviceQrcode.editValue(value));
            
            result.success().message("修改客服二维码成功！");
            
        } catch(Exception e) {
            
            logger.error("#onEditQrcode - ", e);
            
            result.fail().message("修改客服二维码失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display delete qrcode panel
     * 
     * @param qrcodeId
     * @return
     */
    @RequestMapping(value = "/{qrcodeId}/delete" , method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("qrcodeId") Long qrcodeId){
        
        ModelAndView modelAndView = new ModelAndView("system/qrcode/qrcode-delete");
        
        modelAndView.addObject("qrcodeId", qrcodeId);
        
        return modelAndView;
    }
    
    /**
     * Do delete qrcode
     * 
     * @param qrcodeId
     * @return
     */
    @RequestMapping(value = "/{qrcodeId}" , method = RequestMethod.DELETE)
    public @ResponseBody String onDelete(@PathVariable("qrcodeId") Long qrcodeId){
        
        Result result = new Result();
        
        try {
            
            // update qrcode's disabled=false to disabled=true
            serviceQrcodeService.delete(qrcodeId);
            
            result.success().message("删除二维码成功");
            
        } catch (Exception e) {
            
            logger.error("#onDeleteQrcode - " , e);
            
            result.fail().message("删除二维码失败");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }
        
}
